package com.me.videoservice.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.videoservice.model.entry.VideoM3u8Info;
import com.me.videoservice.model.query.M3u8Query;
import com.me.videoservice.service.VideoM3u8Service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/m3u8/")
@CrossOrigin
public class VideoM3u8Controller {

  @Resource
  private VideoM3u8Service videoM3u8Service;

  @GetMapping
  public R<List<VideoM3u8Info>> queryAll() {
    return new R<List<VideoM3u8Info>>().setData(videoM3u8Service.queryAll());

  }

  @PostMapping("page_query")
  public R<Page<VideoM3u8Info>> pageQuery(@RequestBody M3u8Query param) {
    return new R<Page<VideoM3u8Info>>()
        .setData(videoM3u8Service
            .lambdaQuery()
            .like(param.getApp() != null, VideoM3u8Info::getApp, param.getApp())
            .or()
            .like(param.getStream() != null, VideoM3u8Info::getStream, param.getStream())
            .page(new Page<>(param.getPageable().getPage(), param.getPageable().getPageCount())));
  }

  @GetMapping("{id}")
  public R<VideoM3u8Info> detail(@PathVariable String id) {
    return new R<VideoM3u8Info>().setData(videoM3u8Service.getById(id));
  }

}
